function Graph() {
    let vertices = []
    let adjList = []

    this.creatGraph = function () {
        vertices = [0, 1, 2, 3, 4, 5, 6, 7]
        adjList = [
            [0, 1, 1, 1, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 1, 0, 0],
            [1, 0, 0, 0, 1, 0, 1, 0],
            [1, 0, 0, 0, 0, 1, 0, 1],
            [0, 0, 1, 0, 0, 0, 0, 1],
            [0, 1, 0, 1, 0, 0, 1, 0],
            [0, 0, 1, 0, 0, 1, 0, 1],
            [0, 0, 0, 1, 1, 0, 1, 0]
        ]
    }

    function getIndex(vertice) {
        return vertices.findIndex(item => item === vertice)
    }

    function getVertice(vertice) {
        return vertices.find(item => item === vertice)
    }
    this.add = function (vertice, adj) {
        if (getVertice()) {
            console.log('该顶点已存在')
            return
        }
        for (let j in vertices) {
            adjList[j].push(0)
        }
        vertices.push(vertice)
        let verticeIndex = getIndex(vertice)
        let adjVertice = new Array(vertices.length).fill(0)
        for (let i in adj) {
            let index = getIndex(adj[i])
            adjList[index][verticeIndex] = 1
            adjVertice[index] = 1
        }
        adjList.push(adjVertice)
    }
    this.printGraph = function () {
        console.log('顶点集合：--- ' + vertices)
        for (let i in adjList) {
            console.log('邻接矩阵：-' + vertices[i] + '-|' + adjList[i])
        }
    }

    /**
     * 深度优先遍历
     */
    this.DFS = function (start) {
        function isVisited(index) {
            return visited.findIndex(item => item == index)
        }

        function DFS(index) {
            if (isVisited(index) !== -1) {
                return
            }
            result.push(vertices[index])
            visited.push(index)
            for (let i in adjList[index]) {
                if (adjList[index][i] === 1) {
                    DFS(i)
                }
            }
        }
        let index = getIndex(start)
        if (index === undefined) {
            console.log('该起点不存在')
            return
        }
        let visited = []
        let result = []
        DFS(index)
        return result
    }

    /**
     * 广度优先遍历
     */
    this.BFS = function (start) {
        function isVisited(index) {
            return visited.findIndex(item => item == index)
        }

        function BFS(index) {
            if (isVisited(index) !== -1) {
                return
            }
            console.log('BFS:' + index)
            result.push(vertices[next])
            visited.push(index)
            for (let i in adjList[index]) {
                if (adjList[index][i] === 1) {
                    queue.push(i)
                }
            }
        }
        let index = getIndex(start)
        if (index === undefined) {
            console.log('该起点不存在')
            return
        }
        let visited = []
        let result = []
        let queue = []
        let next = start
        while (next !== undefined) {
            BFS(next)
            next = queue.shift()
            console.log('next:' + next)
        }
        return result
    }
}
let graph = new Graph()
graph.creatGraph()
// graph.add(7, [4, 5, 6])
graph.printGraph()
console.log(graph.BFS(0))